package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.mapper.UserMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/changePwd")
public class ChangePwdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        if (!password.equals(password2)) {
            request.setAttribute("user_msg", "两次密码不一致！");
            request.getRequestDispatcher("change_pwd.jsp").forward(request, response);
        } else {
            SqlSession session = SessionUtil.getSqlSession();
            UserMapper userMapper = session.getMapper(UserMapper.class);
            int row=userMapper.changePwd(id, password);
            if(row>0){
                request.getSession().setAttribute("user_msg", "修改成功，请重新登录！");
                response.sendRedirect("login.jsp");
            }
            session.commit();
            session.close();
        }
    }
}